import requests
from bs4 import BeautifulSoup
import re
import bs4

def GETHTMLTEXT(url,code='utf-8'):
    try:
        r=requests.get(url,timeout=20)
        r.raise_for_status
        r.encoding=code
        return r.text
    except:
        return ''

def GETLIST(url):
    soup=BeautifulSoup(GETHTMLTEXT(url),'html.parser')
    start=False
    ls=[]
    lsc=[]
    for tr in soup.dl.children:
        if tr.string == '引子':
            start=True
        if start:
            ls.append(tr.string)
    for i in range(len(ls)):
        if ls[i] != '\n':
            lsc.append(ls[i])
    return lsc

def GETLINK(url):
    soup=BeautifulSoup(GETHTMLTEXT(url),'html.parser')
    start=False
    ls=[]
    lsl=[]
    for tr in soup.dl.children:
        if tr.string == '引子':
            start=True
        if start:
            ls.append(re.search(r'\d{7,8}',str(tr)))
    for i in range(len(ls)):
        if ls[i] != None:
            lsl.append(ls[i].group(0))
    return lsl

def GETINFO():
    num=eval(input('请输入想要查看的章节序号（阿拉伯数字），并按回车键确认：'))
    return num

def GETNOVEL(url):
    text=GETHTMLTEXT(url)
    novellst=[]
    novellst2=[]
    novellst3=[]
    novellst1=re.split('&nbsp;&nbsp;&nbsp;&nbsp;',text)
    for i in range(1,len(novellst1)-1):
        novellst2.append(novellst1[i])
    for i in range(len(novellst2)):
        novellst3.append(novellst2[i].split('<')[0])
    return novellst3

def PRTINFO(lst):
    a=input('请输入数字选择阅读方式（0：编辑器直接阅读；1：桌面生成TXT文档阅读）：')
    if eval(a) == 0:
        for i in range(len(lst)):
            print(lst[i])
        input()
    else:
        with open("C:\\Users\\Lenovo\\Desktop\\novel.txt","w",encoding="utf-8") as f:
            for i in range(len(lst)):
                f.write(lst[i]+'\n')
        print('已在桌面生成小说文档：novel.txt，请前往阅读')
    
def main():
    url='https://www.biquges.com/10_10770/'
    lst=GETLIST(url)
    for i in range(len(lst)):
        print(lst[i])
    num=GETINFO()
    lsl=GETLINK(url)
    table={}
    for i in range(len(lsl)):
        table[i]=lsl[i]
    novelurl=url+table[num]+'.html'
    novellst=GETNOVEL(novelurl)
    if novellst != None:
        print('小说内容爬取成功')
    PRTINFO(novellst)
 
main()
